﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PJAA_Inventory_and_AR___Prototype
{
    public partial class View_Completed_Transactions : Form
    {
        SQLMaster master;
        public View_Completed_Transactions()
        {
            //added this block to make the form transparent
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            InitializeComponent();
            master = new SQLMaster();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ViewComTrans_DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button_Display_Click(object sender, EventArgs e)
        {
            string year_f = comboBoxYear_from.Text.ToString();
            string month_f = comboBoxMonth_from.Text.ToString();
            string day_f = comboBoxDay_from.Text.ToString();

            string year_t = comboBoxYear_to.Text.ToString();
            string month_t = comboBoxMonth_to.Text.ToString();
            string day_t = comboBoxDay_to.Text.ToString();

            if (year_f == "[YYYY]" || month_f == "[MM]" || day_f == "[DD]" || year_t == "[YYYY]" || month_t == "[MM]" || day_t == "[DD]"||
                year_f == "" || month_f == "" || day_f == "" || year_t == "" || month_t == "" || day_t == ""
                || year_f == null || month_f == null || day_f == null || year_t == null || month_t == null || day_t == null)
            {
                MessageBox.Show("Please Complete the details first", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                List<object[]> data = master.loadCompletedTransactions(year_f + "-" + month_f + "-" + day_f, year_t + "-" + month_t + "-" + day_t);

                ViewComTrans_DGV.Rows.Clear();
                

                foreach (object[] strArray in data)
                {

                    ViewComTrans_DGV.Rows.Add(strArray);

                }
            }
        }

        private void Print_CT_Btn_Click(object sender, EventArgs e)
        {
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                    string strExport = "";
                    //Loop through all the columns in DataGridView to Set the 
                    //Column Heading
                    foreach (DataGridViewColumn dc in ViewComTrans_DGV.Columns)
                    {
                        strExport += dc.HeaderText + ",";
                    }
                    strExport = strExport.Substring(0, strExport.Length - 3) + Environment.NewLine.ToString();
                    //Loop through all the row and append the value with 3 spaces
                    foreach (DataGridViewRow dr in ViewComTrans_DGV.Rows)
                    {
                        foreach (DataGridViewCell dc in dr.Cells)
                        {
                            if (dc.Value != null)
                            {
                                strExport += dc.Value.ToString() + ",";
                            }
                        }
                        strExport += Environment.NewLine.ToString();
                    }
                    strExport = strExport.Substring(0, strExport.Length - 3) + Environment.NewLine.ToString();
                    //Create a TextWrite object to write to file, select a file name with .csv extention
                    System.IO.TextWriter tw = new System.IO.StreamWriter(fs);
                    //Write the Text to file
                    tw.Write(strExport);
                    //Close the Textwrite
                    tw.Close();
                }
            }
        }

    }
}
